home *** CD-ROM | disk | FTP | other *** search
- /* sort2.c: Sort strings in descending order */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #define NELEMS 4
-
- static int scomp(const void *, const void *);
-
- main()
- {
- size_t i;
- char *some_strings[NELEMS] =
- {"well","hello","you","rascal"};
-
- qsort(some_strings, NELEMS, sizeof some_strings[0], scomp);
-
- for (i = 0; i < NELEMS; ++i)
- puts(some_strings[i]);
- return 0;
- }
-
- static int scomp(const void *p1, const void *p2)
- {
- char *a = * (char **) p1;
- char *b = * (char **) p2;
-
- /* Negate for descending order */
- return -strcmp(a,b);
- }
-
- /* Output:
- you
- well
- rascal
- hello
- */
-